<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/taglibs.jsp"%>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">  
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="${ctx}/resources/semanticui/semantic.min.css" rel="stylesheet" type="text/css" />
    <link href="${ctx}/resources/apps.css" rel="stylesheet" type="text/css" />
    <title>HR信息</title>  
</head>
<body>
<div class="ui segment" id="n-page">

        <div class="ui segment" id="n-content">
            <form class="ui form">
                <div class="field">
                    <label><span style="color: red">*</span>姓名</label>
                    <div class="ui input">
                        <input type="text" name="name" id="name" placeholder="请输入姓名" value="${employer.name}">
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>英文名</label>
                    <div class="ui input">
                        <input type="text" name="englishname" id="englishname" value="${employer.englishname}" placeholder="请输入英文名">
                    </div>
                </div> 
                <div class="field">
                    <label><span style="color: red">*</span>手机(保密不对外公开)</label>
                    <div class="ui input">
                        <input type="text" name="phone" id="phone" placeholder="请输入您的手机号码" value="${employer.phone}" disabled ="disabled">
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>座机(保密不对外公开)</label>
                    <div class="ui input">
                        <input type="text" name="telephone" id="telephone" placeholder="021-8620xxxx" value="${employer.telephone}">
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>职位</label>
                    <div class="ui input">
                        <input type="text" name="roleName" id="roleName" placeholder="请输入您的职位"  value="${employer.roleName}" >
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>公司邮箱（用于接收邮件）</label>
                    <div class="ui input">
                        <input type="text" name="email" id="email" placeholder="请输入您的公司邮箱"  value="${employer.email}" >
                        <input type="hidden" name="oldemail" id="oldemail" value="${employer.email}" >
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>公司名称</label>
                    <div class="ui search" id="companyname" name="companyname">
                        <div class="ui input">
                            <input class="prompt"  type="text" placeholder="请输入公司名称" id="compName" value="${employer.companyName}">
                            <input type="hidden" name="oldCompName" id="oldCompName" value="${employer.companyName}" >
                        </div>
                        <div class="results"></div>
                    </div>
                </div>
                <div id= "compInfoDiv" name ="compInfoDiv" style="display: none;">
                 <div class="field">
                    <label>公司英文名</label>
                    <div class="ui input">
                        <input type="text" name="companyEname" id="companyEname" value="" placeholder="请输入公司英文名称">
                    </div>
                </div> 
                <div class="field">
                    <label>地址</label>
                    <div class="ui input">
                        <input type="text" name="companyaddress" id="companyaddress" value="" placeholder="请输入公司地址">
                    </div>
                </div>  
                </div>  
                <input type="hidden" name="hrid" id="hrid" value="${employer.id}"/>
            </form>        
        </div>

        <div class="ui center aligned segment" id="choice-buttons">
        	<div class="fluid ui button" id="next">保存</div>
        </div>
</div>

<script src="${ctx}/resources/jweixin-1.0.0.js"></script>
<script src="${ctx}/resources/semanticui/jquery-2.1.4.min.js"></script>
<script src="${ctx}/resources/semanticui/semantic.js" type="text/javascript"></script>
<script type="text/javascript">

wx.config({
    debug: false, 
    appId: "${config.appid}",  
    timestamp: ${config.currenttimestamp},  
    nonceStr: "${config.nonceStr}", 
    signature: "${config.signatureStr}", 
    jsApiList: ['checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideOptionMenu',
        'closeWindow']  
});
 
    var handler = { 
        activate: function() { 
            $(this).addClass('active').siblings().removeClass('active'); 

            if($(this).attr('id') == 'closeit') {
                wx.closeWindow();
            }

            if($(this).attr('id') == 'next')

                var name = $("#name").val();
                var roleName = $("#roleName").val();
                var telephone = $("#telephone").val();
                var phone = $("#phone").val();
                var email = $("#email").val();
                var hrid  = $("#hrid").val();
                var englishname  = $("#englishname").val()
                var companyname = $('#companyname').search('get value');
                if(!name || name=="") {
                    alert("您必须输入您的名字");
                    return false;
                }
                if(name.length > 40) {
                    alert("您输入的名字太长");
                    return false;
                }
                // if(!telephone || telephone=="") {
                //     alert("您必须输入座机号码");
                //     return false;
                // }
                if(telephone.length <5 || telephone.length>18) {
                    alert("您必须输入合格的固定号码");
                    return false;
                }
                var regEx1 = /^([0-9]|[\-])+$/g;
                if (!telephone.match(regEx1)) {
                    alert("您必须输入合格的固定号码");
                    return false;
                }
                if(!phone || phone =="") {
                    alert("您必须输入手机号码");
                    return false;
                }
                var regEx = /^1[0-9][0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/;
                if (!phone.match(regEx)) {
                    alert("您必须输入合格的手机号码");
                    return false;
                }
                if(!email) {
                    alert("您必须输入电子邮箱");
                    return false;
                }
                var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm;
                if(!re.test(email)) {
                    alert("您必须输入合格的电子邮箱");
                    return false;
                }
                if(!companyname || companyname=="") {
                    alert("您必须输入公司名称");
                    return false;
                }
                if(!roleName || roleName=="") {
                    alert("您必须输入您的职位");
                    return false;
                }
                if(!englishname || englishname=="") {
                    alert("您必须输入您的英文名");
                    return false;
                }

                var oldemail = $("#oldemail").val();
                var oldCompName = $("#oldCompName").val();
                if (oldemail != email && oldCompName == companyname) {
                    alert("您已修改邮箱，需同时修改公司名称");
                    return false;
                }
                if (oldemail == email && oldCompName != companyname) {
                    alert("您已修改公司名称，需同时修改邮箱");
                    return false;
                }


                var that = this;

                var postUrl = '${ctx}/wechat/employer/create.do';
                var param = {};
                param['name']  = name;
                param['roleName']  = roleName;
                param['telephone'] = telephone;
                param['email']  = email;
                param['phone']  = phone;
                param['englishname']= englishname;
                param['companyEname']= $("#companyEname").val();
                param['companyaddress']= $("#companyaddress").val();
                param['companyName'] = companyname;
                
                if (hrid) { 
                    param['id'] = hrid;
                    var b = window.confirm("提交个人信息后，账号需重新认证审核，预计需要1-5个工作日，是否确定提交个人信息？");
                    if (!b) {
                        return ;
                    }
                }

                $.ajax({ 
                    url: postUrl, 
                    type: "POST", 
                    data: param,
                    success: function(data) { 
                        if(data.errorcode=="0"){
                           //新公司名称，创建公司
                            var employer = data.data;
                            location.href = '${ctx}/wechat/company/toCreate.page?employerId='+employer.id;
                            return;
                        }  else if(data.errorcode=="1") {
                           //公司名称存在，创建成功，跳转进入上传图片
                            alert(data.msg);
                            <%--location.href = '${ctx}/wechat/hr/toUploadFile.page';--%>
                            wx.closeWindow();
                            return;
                        } else {
                            alert(data.msg);
                            $(that).removeClass('disabled');
                        }
                    }, 
                    error: function(){ 
                        alert("服务出错，请稍后尝试"); 
                        $(that).removeClass('disabled');
                    } 
                });
            }

    };
	var companydata = [];
    
    <c:forEach items="${companies}" var="company">
        var tmp${company.id} = {};
        tmp${company.id}.title = "${company.name}";
        companydata.push(tmp${company.id});
    </c:forEach>

    $(document).ready(function() { 
      //$( "#companyname" ).bind( "change", function(event, ui) {
      //  var companyname = $('#companyname').search('get value');
      //  alert(companyname);
      //});
            $('.button').on('click', handler.activate);
            
            $("#email").bind('input propertychange',function(e){
                console.log($(this).val())
                var email = $("#oldemail").val();
                var companyname = $('#companyname').search('get value');
                if(email!=null){
	               	if(email==$(this).val()){
                    	// alert("您已修改邮箱，需同时修改公司名称");
                    	// $('#companyname').val("");
                    }
                }
            })
            
            $('.ui.search').search({
              source: companydata,
              searchFullText: true
          });
          wx.ready(function() {
            wx.hideOptionMenu();
        });
    });

</script>
</body>
</html>